package com.wyp168.juc;

/**
 * @ClassName DeadLockDemo001
 * @Description TODO
 * @Author wyp168
 * @Date 2022/7/8 15:34
 */
public class DeadLockDemo001 {
    public static volatile Object objectA = new Object();
    public static volatile Object objectB = new Object();

    public static void main(String[] args) {
        Thread threadA = new Thread(() -> {
            System.out.println("ThreadA try to get objectA lock...");
            synchronized (objectA) {
                System.out.println("ThreadA has gotten objectA lock!");

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("ThreadA waiting objectA lock");

                synchronized (objectB) {
                    System.out.println("ThreadA has gotten objectB lock!");
                }
            }
        });

        Thread threadB = new Thread(() -> {
            System.out.println("ThreadB try to get objectB lock...");
            synchronized (objectB) {
                System.out.println("ThreadB has gotten objectB lock!");

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("ThreadB waiting objectA lock");
                synchronized (objectA) {
                    System.out.println("ThreadB has gotten objectA lock!");
                }
            }
        });

        threadA.start();
        threadB.start();


    }
}
